package com.sskj.one.view;

import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.sskj.lib.R2;
import com.sskj.lib.util.ScreenUtil;
import com.sskj.one.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class TipDialog extends AlertDialog {

    @BindView(R.id.tv_content)
    TextView tv_content;
    @BindView(R.id.tv_cancel)
    TextView tv_cancel;
    @BindView(R.id.tv_confirm)
    TextView tv_confirm;
    private ClickListener listener;
    private LeftClickListener leftClickListener;

    public TipDialog(Context context) {
        super(context, R.style.lib_custom_dialog);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_common, null);
        setView(view);
        ButterKnife.bind(this, view);
        tv_confirm.setOnClickListener(v -> {
            if (listener != null) {
                listener.onClick(this);
            }
            this.dismiss();
        });
        tv_cancel.setOnClickListener(v -> {
            if (leftClickListener != null) {
                leftClickListener.onClick(this);
            } else {
                this.dismiss();
            }
        });
    }


    public TipDialog setCancel(boolean canceLAble){
        setCancelable(canceLAble);
        return this;
    }


    public TipDialog setContentText(String content) {
        tv_content.setText(content);
        return this;
    }

    public TipDialog setCancelText(String text) {
        tv_cancel.setText(text);
        return this;
    }

    public TipDialog setConfirmText(String text) {
        tv_confirm.setText(text);
        return this;
    }

    public TipDialog setCancelVisible(int visible) {
        tv_cancel.setVisibility(visible);
        return this;
    }


    public interface ClickListener {
        void onClick(TipDialog dialog);
    }

    public interface LeftClickListener {
        void onClick(TipDialog dialog);
    }

    public TipDialog setListener(ClickListener clickListener) {
        this.listener = clickListener;
        return this;
    }

    public TipDialog setLeftClickListener(LeftClickListener leftClickListener) {
        this.leftClickListener = leftClickListener;
        return this;
    }

    @Override
    public void show() {
        super.show();
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.width = (int) (ScreenUtil.getScreenWidth(getContext()) * 0.9);
        getWindow().setAttributes(layoutParams);
    }
}
